package com.jason.weibosender.ui;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputMethodEvent;
import java.awt.event.InputMethodListener;
import java.io.File;
import java.util.List;

import javax.jws.Oneway;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

import com.jason.weibosender.util.TokenCheckerHelper;

import weibo4j.Friendships;
import weibo4j.Oauth;
import weibo4j.http.AccessToken;
import weibo4j.model.User;
import weibo4j.model.UserWapper;
import weibo4j.model.WeiboException;
import weibo4j.util.BareBonesBrowserLaunch;
import weibo4j.util.WeiboConfig;

public class CodeFrame extends JFrame implements ActionListener, CaretListener{
	
	JTextArea textArea;
	JScrollPane scrollContent;
	JPanel buttonPanel;
	JButton button;
	JButton buttonOauth;
	Oauth oauth;
	AccessToken accessToken;
	public CodeFrame(){
		initComponents();
	}
	private void initComponents(){
		textArea = new JTextArea();
		textArea.addCaretListener(this);
		scrollContent = new JScrollPane(textArea);
		button = new JButton("获取验证码");
		buttonOauth = new JButton("绑定");
		button.addActionListener(this);
		buttonOauth.addActionListener(this);
		buttonOauth.setEnabled(false);
		buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
		buttonPanel.add(button);
		buttonPanel.add(buttonOauth);
		this.setLayout(new BorderLayout());
		this.add(scrollContent, BorderLayout.CENTER);
		this.add(buttonPanel, BorderLayout.SOUTH);
		this.setSize(500, 200);
		this.setLocationRelativeTo(null);
		this.setIconImage(
				new ImageIcon(
						"." + File.separator + "icon" + File.separator + "weibo.jpg").getImage());
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == button){
			oauth = new Oauth();
			try {
				BareBonesBrowserLaunch.openURL(oauth.authorize("code"));
			} catch (WeiboException e1) {
				e1.printStackTrace();
			}
		} else if(e.getSource() == buttonOauth){
			if(oauth == null){
				oauth = new Oauth();
			}
			try {
				accessToken = oauth.getAccessTokenByCode(textArea.getText());
				if( ! accessToken.getAccessToken().isEmpty()){
					long size = TokenCheckerHelper.checkTokenAvaliableTime(accessToken.getAccessToken());
					if(size != -1){
						System.out.println("accessToken.getAccessToken()" + accessToken.getAccessToken());
						WeiboConfig.updateProperties("accessToken", accessToken.getAccessToken());
						this.dispose();
						new ClientFrame(accessToken.getAccessToken(), size);
					}
				}
			} catch (WeiboException e1) {
				e1.printStackTrace();
			}
		}
	}
	@Override
	public void caretUpdate(CaretEvent e) {
		EventQueue.invokeLater(new Runnable() {
			
			@Override
			public void run() {
				if(textArea.getText() != null && textArea.getText().length() > 0){
					buttonOauth.setEnabled(true);
					button.setEnabled(false);
				} else {
					buttonOauth.setEnabled(false);
					button.setEnabled(true);
				}
			}
		});
	}
}
